﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using OpenQuarters.WebQuarters.Utils;
using System.Text;
using System.Xml;

namespace OpenQuarters.WebQuarters.Core
{
    public class ContentDefinition
    {
        private List<ContentAreaDefinition> areas = new List<ContentAreaDefinition>();

        public List<ContentAreaDefinition> Areas
        {
            get { return areas; }
            set { areas = value; }
        }

        public ContentAreaDefinition this[string area]
        {
            get
            {
                var r = Areas.FirstOrDefault(a => a.Id == area);
                if (r == null)
                {
                    r = new ContentAreaDefinition()
                    {
                        Id = area
                    };
                    this.Areas.Add(r);
                }
                return r;
            }
            set
            {
                Areas.Where(a => a.Id == area).ToList().ForEach(a => Areas.Remove(a));
                value.Id = area;
                this.Areas.Add(value);
            }
        }

        public ContentDefinition()
        {
        }

        public static ContentDefinition FromJSON(string json)
        {
            return json == null ? new ContentDefinition() : GeneralUtils.FromJSON<ContentDefinition>(json);
        }

        public string ToJSON()
        {
            return GeneralUtils.ToJSON(this);
        }
    }
}
